package com.ailink.concurrent.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

@Slf4j
public class TestAQS {
    public static void main(String[] args) throws Exception {
        ReentrantLock lock = new ReentrantLock(true);
        Condition condition = lock.newCondition();
        new Thread(() -> {
            try {
                log.info("lock.lock()");
                lock.lock();
                log.info("condition.await()");
                Thread.sleep(3000);
                condition.await();
                log.info("end");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }).start();
        Thread.sleep(1000);
        try {
            log.info("lock-1");
            lock.lock();
            log.info("lock-2");
            condition.signal();
        } finally {
            lock.unlock();
        }
    }
}
